package cn.iocoder.yudao.module.game.controller.admin.mail;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.game.controller.admin.mail.vo.*;
import cn.iocoder.yudao.module.game.dal.dataobject.mail.MailDO;
import cn.iocoder.yudao.module.game.service.mail.MailService;

@Tag(name = "管理后台 - 邮件")
@RestController
@RequestMapping("/game/mail")
@Validated
public class MailController {

    @Resource
    private MailService mailService;

    @PostMapping("/create")
    @Operation(summary = "创建邮件")
    @PreAuthorize("@ss.hasPermission('game:mail:create')")
    public CommonResult<Long> createMail(@Valid @RequestBody MailSaveReqVO createReqVO) {
        return success(mailService.createMail(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新邮件")
    @PreAuthorize("@ss.hasPermission('game:mail:update')")
    public CommonResult<Boolean> updateMail(@Valid @RequestBody MailSaveReqVO updateReqVO) {
        mailService.updateMail(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除邮件")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('game:mail:delete')")
    public CommonResult<Boolean> deleteMail(@RequestParam("id") Long id) {
        mailService.deleteMail(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得邮件")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('game:mail:query')")
    public CommonResult<MailRespVO> getMail(@RequestParam("id") Long id) {
        MailDO mail = mailService.getMail(id);
        return success(BeanUtils.toBean(mail, MailRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得邮件分页")
    @PreAuthorize("@ss.hasPermission('game:mail:query')")
    public CommonResult<PageResult<MailRespVO>> getMailPage(@Valid MailPageReqVO pageReqVO) {
        PageResult<MailDO> pageResult = mailService.getMailPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MailRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出邮件 Excel")
    @PreAuthorize("@ss.hasPermission('game:mail:export')")
    @OperateLog(type = EXPORT)
    public void exportMailExcel(@Valid MailPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MailDO> list = mailService.getMailPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "邮件.xls", "数据", MailRespVO.class,
                        BeanUtils.toBean(list, MailRespVO.class));
    }

}